package com.honghe.web.controller.system;

import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.honghe.common.core.controller.BaseController;
import com.honghe.common.core.domain.AjaxResult;
import com.honghe.common.core.page.TableDataInfo;
import com.honghe.system.domain.vo.CustomerContactStatsVO;
import com.honghe.system.domain.CustomerContactRecord;
import com.honghe.system.service.ICustomerContactStatsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 客户联系记录统计
 */
@RestController
@RequestMapping("/system/customer/contact/stats")
public class CustomerContactStatsController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(CustomerContactStatsController.class);

    @Autowired
    private ICustomerContactStatsService customerContactStatsService;

    /**
     * 获取联系记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer:stats')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerContactStatsVO customerContactStats) {
        try {
            startPage();
            List<CustomerContactRecord> list = customerContactStatsService.selectContactList(customerContactStats);
            return getDataTable(list);
        } catch (Exception e) {
            log.error("获取联系记录列表失败", e);
            return new TableDataInfo();
        }
    }

    /**
     * 获取统计数据
     */
    @PreAuthorize("@ss.hasPermi('system:customer:stats')")
    @GetMapping("")
    public AjaxResult getStats(CustomerContactStatsVO customerContactStats) {
        try {
            CustomerContactStatsVO stats = customerContactStatsService.getContactStatistics(customerContactStats);
            log.info("获取统计数据成功: {}", stats);
            return success(stats);
        } catch (Exception e) {
            log.error("获取统计数据失败", e);
            return error("获取统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取负责人统计数据
     */
    @PreAuthorize("@ss.hasPermi('system:customer:stats')")
    @GetMapping("/handler")
    public AjaxResult getHandlerStats(CustomerContactStatsVO customerContactStats) {
        try {
            List<Map<String, Object>> stats = customerContactStatsService.getHandlerContactStats(customerContactStats);
            log.info("获取负责人统计数据成功");
            return success(stats);
        } catch (Exception e) {
            log.error("获取负责人统计数据失败", e);
            return error("获取负责人统计数据失败: " + e.getMessage());
        }
    }
} 